package com.cy.src.myapplication;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

import java.io.File;

/**
 * Created by CY on 2016/9/20.
 */

public class Zip4jHelper {

    public static void main(String[] args) {
        try {
            new Zip4jHelper().zipProjects();
            System.out.println("zip finish.");
        } catch (ZipException e) {
            e.printStackTrace();
        }
    }

    /**
     * 压缩E盘
     */
    public void zipE() throws ZipException {
//        E:\

//        File[] listRoots = File.listRoots();
//        for (File f : listRoots) {
//            System.out.println("f=" + f.getPath()+"  "+f.exists());
//            if (f.getPath().startsWith("E")){
//
//                break;
//            }
//        }
//        zip( "E:\\CYWork","E:\\CYWork.zip");


        String dir = "E:\\CYWork";

        File[] listFiles = new File(dir).listFiles();

        for (final File file : listFiles) {
            System.out.println(" file " + file.getPath());
            if (file.isDirectory()) {
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        try {
                            System.out.println(" compass start.");

                            File zip = new File(file.getParent(), file.getName() + ".zip");
                            zip(file.getPath(), zip.getPath());
                            System.out.println(zip.getPath() + " compass finish.");
                        } catch (ZipException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();

            }
        }

    }

    public void zipProjects() throws ZipException {
        String folderToAdd = "E:\\CYWork\\Projects";
        String zip = "E:\\CYWork\\Projects.zip";
        zip(folderToAdd, zip);
    }
    public void zipNVS() throws ZipException {
        String folderToAdd = "E:\\CYWork\\Projects\\k201\\codes\\NVS201_AndroidStudio\\NVS201";
        String targetFile = "E:\\CYWork\\Projects\\k201\\codes\\NVS201_AndroidStudio\\NVS201.zip";
        zip(folderToAdd, targetFile);
    }

    public void zipMuxMedia() throws ZipException {
        String targetFile = "E:\\WorkStudio\\MuxMedia-ZIP.zip";
        String folderToAdd = "E:\\WorkStudio\\MuxMedia";
        zip(folderToAdd, targetFile);
    }

    private void zip(String folderToAdd, String targetZipFile) throws ZipException {
        ZipFile zipFile = new ZipFile(targetZipFile);
        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        zipFile.addFolder(folderToAdd, parameters);
    }
}
